package com.cashbook.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cashbook.mapper.TbInExMapper;
import com.cashbook.pojo.TbInEx;
import com.cashbook.pojo.TbInExExample;
import com.cashbook.pojo.TbInExExample.Criteria;
import com.cashbook.service.InExService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import entity.PageResult;
@Service
public class InExServiceImpl implements InExService{
	@Autowired
	TbInExMapper inexMapper;
	/**
	 * 查询全部
	 */
	@Override
	public List<TbInEx> findAll() {
		return inexMapper.selectByExample(null);
	}
	/**
	 * 分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page <TbInEx> page=(Page<TbInEx>) inexMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}
	/**
	 * 添加
	 */
	@Override
	public void add(TbInEx inex) {
		inexMapper.insert(inex);
		
	}
	/**
	 * 更新
	 */
	@Override
	public void update(TbInEx inex) {
		inexMapper.updateByPrimaryKey(inex);
		
	}
	/**
	 * 根据主键查询
	 */
	@Override
	public TbInEx findOne(Long id) {
		return inexMapper.selectByPrimaryKey(id);
	}
	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids) {
			inexMapper.deleteByPrimaryKey(id);
		}
		
	}
	/**
	 * 分页查询
	 */
	@Override
	public PageResult findPage(TbInEx inex, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		TbInExExample example=new TbInExExample();
		Criteria criteria = example.createCriteria();
		if(inex!=null) {
			if(inex.getCateId()!=null) {
				criteria.andCateIdEqualTo(inex.getCateId());
			}
			if(inex.getMark()!=null && inex.getMark().length()>0) {
				criteria.andMarkLike("%"+inex.getMark()+"%");
			}
			if(inex.getNum()!=null) {
				criteria.andNumEqualTo(inex.getNum());
			}
			if(inex.getOpenId()!=null && inex.getOpenId().length()>0) {
				criteria.andOpenIdLike("%"+inex.getOpenId()+"%");
			}
			if(inex.getTime()!=null) {
				criteria.andTimeEqualTo(inex.getTime());
			}
			if(inex.getTypeName()!=null && inex.getTypeName().length()>0) {
				criteria.andTypeNameLike("%"+inex.getTypeName()+"%");
			}
			if(inex.getWeek()!=null && inex.getWeek().length()>0) {
				criteria.andWeekLike("%"+inex.getWeek()+"%");
			}
		}
		Page <TbInEx> page=(Page<TbInEx>) inexMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	/**
	 * 模糊查询
	 */
	@Override
	public List<TbInEx> searchByDetail(String time, Long cateId, String openId) {
		TbInExExample example=new TbInExExample();
		Criteria criteria = example.createCriteria();
		SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
		if(!"".equals(time)&&time!=null&& cateId!=null && !"".equals(openId)&&openId!=null) {
			//criteria.andTimeBetween(sm.parse(time), new Date(time+" "))
			try {
				//criteria.andTimeGreaterThanOrEqualTo(sm.parse(time));
				//criteria.andTimeLessThanOrEqualTo(sm.parse(time));
				criteria.andTimeLike("%"+time+"%");
			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
			criteria.andCateIdEqualTo(cateId);
			criteria.andOpenIdLike("%"+openId+"%");
		}
		return inexMapper.selectByExample(example);
	}
	/**
	 * 查询年账单 年echart图
	 */
	@Override
	public List<Map<String, Object>> selectBytypeName(TbInEx inex) {
		
		return inexMapper.selectBytypeName(inex);
	}
	/**
	 * 查询年 月echart图
	 */
	@Override
	public List<Map<String, Object>> selectEchartByMonth(TbInEx record) {
		
		return inexMapper.selectEchartByMonth(record);
	}
	/**
	 * 周echart图
	 */
	@Override
	public List<Map<String, Object>> selectEchartByWeek(String openId, Date begintime, Date endtime, Long cateId) {
		
		return inexMapper.selectEchartByWeek(openId, begintime, endtime, cateId);
	}
	/**
	    * 查询所有 包括tb_user表的信息
	 */
	@Override
	public PageResult selectAll(int pageNum,int pageSize,TbInEx inex) {
		/*PageHelper.startPage(pageNum, pageSizes);
		Page <TbInEx> page=
		return new PageResult(page.getTotal(),pageinexMapper.selectAll());*/
		   
		// List<TbInEx> selectByExample(TbInExExample example);
		//
		PageHelper.startPage(pageNum, pageSize);
		Page <List<Map<String,Object>>> page= (Page ) inexMapper.selectAll(inex);
		return new PageResult(page.getTotal(), page.getResult());
	}
	

}
